import base64

from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA1

message = "I met aliens in UFO. Here is the map."


def sign(data):
    with open("private.pem") as private_pem:
        private_key = private_pem.read()
        rsa_private_key = RSA.importKey(private_key)

        signer = PKCS1_v1_5.new(rsa_private_key)
        signature = signer.sign(SHA1.new(data.encode()))
        signature_base64 = base64.b64encode(signature)
        print(signature_base64.decode())
        return signature_base64


def verify(data, signature):
    with open("public.pem") as public_pem:
        public_key = public_pem.read()
        ras_public_key = RSA.importKey(public_key)
        verifier = PKCS1_v1_5.new(ras_public_key)
        if verifier.verify(SHA1.new(data.encode()), base64.b64decode(signature)):
            return True
        return False


# sign(message)
print(verify(message, sign(message)))
